﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;

namespace Shop
{
    public static class NameValueExt
    {
        #region 把Form中的NameValueCollection，转换为LIST，方便程序调用
        public static List<Dictionary<string, object>> GetList(this NameValueCollection nv, string tablename)
        {
            string[] keysValue = nv.AllKeys.Where(key => key.StartsWith(tablename)).ToArray();
            if (keysValue.Length == 0) { return null; }
            int tmpint=0;
            int imax = 0;
            for (int i = 0; i < keysValue.Length; i++)
            {
                string[] s2 = keysValue[i].Split(new string[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
                if (s2.Length < 2) { tmpint=0; }
                else { tmpint = s2[1].GetVal_Int(); }
                if (imax < tmpint) { imax = tmpint; }
            }
            //string keysnamemax = keysValue.Max();
            //string[] s2 = keysnamemax.Split(new string[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
            //if (s2.Length < 2) { return null; }
            //int imax = int.Parse(s2[1]);
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            Dictionary<string, object> dic;
            for (int i = 0; i <= imax; i++)
            {
                string[] skeyinput = keysValue.Where(key => key.StartsWith(tablename + "[" + i.ToString() + "]")).ToArray();
                Array.Sort(skeyinput);
                dic = new Dictionary<string, object>();
                for (int k = 0; k < skeyinput.Length; k++)
                {

                    string[] s2 = skeyinput[k].Split(new string[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
                    if (s2.Length >= 3)
                    { dic.Add(s2[2], nv[skeyinput[k]]); }
                }
                list.Add(dic);
            }
            return list;
        }
        #endregion

        public static DataTable GetListToDataTable(this NameValueCollection nv, string tablename, DataTable dtsource)
        {
            var list = nv.GetList(tablename);
            if (list.Count > 0)
            {
                foreach (Dictionary<string, object> dictionary in list)
                {
                    DataRow dtr = dtsource.NewRow();
                    foreach (string current in dictionary.Keys)
                    {
                        for (int j = 0; j < dtsource.Columns.Count; j++)
                        {
                            if (current == dtsource.Columns[j].ColumnName)
                            {
                                dtr[j] = dictionary[current];
                            }
                        }
                    }
                    dtsource.Rows.Add(dtr);
                }
            }
            return dtsource;
        }

        public static string getnvVal_String(this NameValueCollection nv)
        {
            string s = "";
            foreach (string key in nv.AllKeys)
            {
                s += key + "|" + string.Join(",", nv.GetValues(key)) + ";";
            }
            return s;
        }

    }
}